﻿Imports ISNet.WebUI.WebTreeView
Imports log4net

Partial Class R19
    Inherits System.Web.UI.Page
    Public ScriptText As String = ""
    Private Shared logger As ILog = LogManager.GetLogger("Report_R19")

    Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
        wcFundcenter3.EncodeOutput = True
    End Sub
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        Label1.Text = "ระบบจัดทำงบประมาณ >> รายงาน >> รายงานสรุปโครงการและงานสนับสนุน"
        If IsPostBack = False Then
            Try
                Dim adap As New DBdataTableAdapters.AppConfigsTableAdapter
                Dim dt As New DBdata.AppConfigsDataTable
                dt = adap.GetData
                For Each item As DBdata.AppConfigsRow In dt
                    Select Case item.Name
                        Case "ACTIVE_PLAN_YEAR"
                            PlanYearsTxt.Text = item.Value
                            Exit For
                    End Select
                Next
                Dim role_type As String = ""
                Dim usadap As New UserDSTableAdapters.Users2TableAdapter
                role_type = usadap.GetRoleType(role_type)
                Dim vtext As String = "1"
                Select Case role_type
                    Case "2"
                        vtext = "1"
                    Case "3"
                        vtext = "2"
                    Case "4"
                        vtext = "3"
                    Case "5"
                        vtext = "4"
                    Case Else
                        vtext = "1"
                End Select
                VersionTxt.Text = vtext
            Catch ex As Exception
                logger.Error(ex.Message)
                logger.Error(ex.StackTrace)
            End Try
        End If
    End Sub
    'Sub PlotChild(ByVal gpath As String, ByRef cnode As WebTreeViewNode, ByVal level As Decimal)
    '    Dim Hadapter As New FundCenterParentTableAdapters.FctHTableAdapter
    '    Dim Hdatatable As New FundCenterParent.FctHDataTable
    '    Hdatatable = Hadapter.GetDataByGPath(gpath, Val(Session("planyear")))

    '    Dim MyNode As WebTreeViewNode
    '    MyNode = New WebTreeViewNode()
    '    For Each row As FundCenterParent.FctHRow In Hdatatable
    '        MyNode.Text = row.Description
    '        MyNode.Value = row.FundCenter_FundCenterCode
    '        cnode.Nodes.Add(MyNode)
    '    Next
    '    If Len(gpath) >= 10 Then
    '        Exit Sub
    '    End If
    '    Dim hadap2 As New FundCenterParentTableAdapters.UserLikeGpathTableAdapter
    '    Dim hdt2 As New FundCenterParent.UserLikeGpathDataTable
    '    Dim sql As String = ""
    '    Dim prefil As String = ""
    '    prefil = ""

    '    Dim pathadd As String = "__"
    '    sql = ""
    '    Dim fullpath As String = gpath & pathadd
    '    If prefil <> "" Then
    '        sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
    '    End If
    '    sql += " AND grouppath like '" & fullpath & "' "
    '    sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND Fcth.PlanYear = " & Val(Session("PlanYear"))
    '    hadap2.GetDataByFilter(hdt2, sql)

    '    For Each row As FundCenterParent.UserLikeGpathRow In hdt2
    '        If Len(row.grouppath) <= 10 Then
    '            PlotChild(row.grouppath, MyNode, level + 1)
    '        End If
    '    Next
    'End Sub
    Protected Sub wcFundcenter1_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles wcFundcenter1.InitializeDataSource
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        Dim enableAllReport As Boolean = False
        enableAllReport = Session("EnableAllReport")
        If enableAllReport Then
            sql += " WHERE 1 = 1 "
            sql += " and FundCenters.PlanYear = " & Val(Session("planyear"))
            sql += " and fctr_view.PlanYear = " & Val(Session("planyear"))
            'sql += " and FundCenterHierarchies.FundCenterGroup = " & Session("FCTRgroup")
            sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("planyear")) & " "
            sql += " and fctr_view.ParentFundCenter = 0 "
            sql += " GROUP BY fctr_view.FundCenterCode, FundCenters.Description, FundCenterHierarchies.Level,fctr_view.ParentFundCenter "
            sql += " ORDER BY FundCenterCode ASC"
        Else
            sql += " WHERE 1 = 1 "
            sql += " AND UserName = '" & Session("users") & "'"
            sql += " and FundCenters.PlanYear = " & Val(Session("planyear"))
            sql += " and fctr_view.PlanYear = " & Val(Session("planyear"))
            'sql += " and FundCenterHierarchies.FundCenterGroup = " & Session("FCTRgroup")
            sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("planyear")) & " "
            sql += " ORDER BY FundCenterCode ASC"
        End If
        adap.GetDataByFilter(dt, sql)
        dt.Rows.Add("", "ทุกรายการ", "1", "0")
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt
    End Sub
    Protected Sub wcFundcenter2_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles wcFundcenter2.InitializeDataSource
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        Dim enableAllReport As Boolean = False
        enableAllReport = Session("EnableAllReport")
        If enableAllReport Then
            sql += " WHERE 1 = 1 "
            sql += " and FundCenters.PlanYear = " & Val(Session("planyear"))
            sql += "  and fctr_view.PlanYear = " & Val(Session("planyear"))
            'sql += " and FundCenterHierarchies.FundCenterGroup = " & Session("FCTRgroup")
            sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("planyear")) & " "
            sql += " and fctr_view.ParentFundCenter = 0 "
            sql += " GROUP BY fctr_view.FundCenterCode, FundCenters.Description, FundCenterHierarchies.Level,fctr_view.ParentFundCenter "
            sql += " ORDER BY FundCenterCode ASC"
        Else
            sql += " WHERE 1 = 1 "
            sql += " AND UserName = '" & Session("users") & "'"
            sql += " and FundCenters.PlanYear = " & Val(Session("planyear"))
            sql += "  and fctr_view.PlanYear = " & Val(Session("planyear"))
            'sql += " and FundCenterHierarchies.FundCenterGroup = " & Session("FCTRgroup")
            sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("planyear")) & " "
            sql += " ORDER BY FundCenterCode ASC"
        End If
        adap.GetDataByFilter(dt, sql)
        dt.Rows.Add("", "ทุกรายการ", "1", "0")
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt
    End Sub
    Protected Sub toReport_Click(sender As Object, e As ImageClickEventArgs) Handles toReport.Click
        If Val(PlanYearsTxt.Text) = 0 Then
            ScriptText = "<script language='javascript'>alert('กรุณาระบุปีงบประมาณ');</script>"
            Exit Sub
        End If
        If VersionTxt.Text.Trim = "" Then
            ScriptText = "<script language='javascript'>alert('กรุณาระบุ Version');</script>"
            Exit Sub
        End If
        Session.Remove("year")
        Session.Add("year", PlanYearsTxt.Text)
        Session.Remove("version")
        Session.Add("version", VersionTxt.Text)
        Session.Remove("code")
        Session.Add("code", ddlFund.SelectedValue)
        Session.Remove("projectCode")
        Session.Add("projectCode", txtProjectCode.Text)
        Session.Remove("FundcenterAll")

        'Add by Orn 28/4/2558
        Dim useradap As New UserDSTableAdapters.UsersTableAdapter()
        Dim userDS As New UserDS.UsersDataTable
        userDS = useradap.GetDataByUsername(Session("users"))
        Dim enableAllUser As Boolean = False
        If userDS.Rows.Count > 0 Then
            If userDS(0)("Enable_All_Report_Flag").ToString() <> "" Then
                enableAllUser = userDS(0)("Enable_All_Report_Flag")
            End If
        End If
        If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
            Session.Remove("FCTRmode")
            Session.Add("FCTRmode", "1")
            Session.Remove("FundcenterAll")
            If enableAllUser = True Then
                Session.Add("FundcenterAll", FctrClass.GetFromToFundCenterReport(wcFundcenter1.Value, wcFundcenter2.Value, Val(Session("year"))))
            Else
                Session.Add("FundcenterAll", FctrClass.GetUserFundCenterFromTo(Session("users"), Val(Session("year")), wcFundcenter1.Value, wcFundcenter2.Value))
            End If
        ElseIf TabContainer1.ActiveTab.ID = TabPanel2.ID Then
            Session.Remove("FCTRmode")
            Session.Add("FCTRmode", "2")
            If wcFundcenter3.Value <> "" Then
                If wcFundcenter3.Value <> 0 Then
                    Session.Add("FundcenterAll", FctrClass.GetSubFundCenterrReport(wcFundcenter3.Value, Session("users"), Val(Session("year")), ""))
                Else
                    If enableAllUser = True Then
                        Session.Add("FundcenterAll", FctrClass.GetUserFundCenter(Val(Session("year"))))
                    Else
                        Session.Add("FundcenterAll", FctrClass.GetUserFundCenter(Session("users"), Val(Session("year"))))
                    End If
                End If
            Else
                If enableAllUser = True Then
                    Session.Add("FundcenterAll", FctrClass.GetUserFundCenter(Val(Session("year"))))
                Else
                    Session.Add("FundcenterAll", FctrClass.GetUserFundCenter(Session("users"), Val(Session("year"))))
                End If
            End If
        End If
        'End Add by Orn 28/4/2558
        ScriptText = "<script language='javascript'>openpop();</script>"
    End Sub
    Protected Sub wcFundcenter3_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles wcFundcenter3.InitializeDataSource
        Dim adap As New FundCenterParentTableAdapters.SelectorTableAdapter
        Dim dt As New FundCenterParent.SelectorDataTable
        Dim dt2 As New FundCenterParent.SelectorDataTable
        Dim sql = ""
        Dim enableAllReport As Boolean = False
        enableAllReport = Session("EnableAllReport")
        If enableAllReport Then
            sql += " WHERE 1 = 1 "
            sql += " AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FctH.PlanYear = " & Val(Session("PlanYear"))
            'sql += " and FctH.fundcentergroup = " & Session("FCTRgroup")
            sql += " GROUP BY  FctH.Description, FctH.FundCenter_FundCenterCode, FctH.ParentFundCenterHierarchy_Id"
            sql += ", FctH.grouppath,  FctH.pathname, FctH.planyear "
            sql += " ORDER BY Grouppath"
        Else
            sql += " WHERE 1 = 1 "
            sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FctH.PlanYear = " & Val(Session("PlanYear"))
            'sql += " and FctH.fundcentergroup = " & Session("FCTRgroup")
            sql += " ORDER BY Grouppath"
        End If
        adap.GetDataByFilter(dt2, sql)
        dt.Rows.Add("ทุกรายการ", "0", 0, "0", Val(Session("PlanYear")), "ทุกรายการ")
        dt.Merge(dt2)
        e.DataSource = dt
    End Sub
End Class
